﻿window.Model = function ()
{
    this.loadNewChartData = function (count, base, spread)
    {
        count = Math.round(count) || 30;
        base = Math.round(base) || 100;
        spread = Math.round(spread) || 20;

        var prev = base;
        var points = new Array(count);
        for (var i = 0; i < count; i++)
        {
            points[i] = prev + Math.round(Math.random() * spread) - spread / 2;
            prev = points[i];
        }
        return points;
    };
};

function renderDataGrid(values, cols)
{
    if (!values) return null;
    cols = cols || 10;

    var table = $('<table />');
    for (var i = 0; i < values.length; i += cols)
    {
        var row = $('<tr />');
        row.append($('<td />').text((i + 1) + '..' + (i + cols) + ': '));
        for (var j = 0; j < cols; j++)
        {
            var cell = $('<td />').text(values[i + j]);
            row.append(cell);
        }
        table.append(row);
    }
    return table;
}

function safeArrayItemByIndex(array, index)
{
    if (!array || !array.length || index == undefined) return null;
    if (typeof index == 'string')
    {
        if (index == 'first') index = 0;
        else if (index == 'last') index = array[array.length - 1];
    }
    var safeIndex = Math.max(0, Math.min(array.length - 1, Math.floor(index)));
    return array[safeIndex];
}

function getSafeIndex(array, index)
{
    if (!array) return index;
    var safeIndex = Math.max(0, Math.min(array.length - 1, Math.floor(index)));
    return safeIndex;
}

function getSafeRange(array, range)
{
    if (!array || !array.length) return null;
    if (!range || !range.length) return null;
    var safeRange = [getSafeIndex(array, range[0]), getSafeIndex(array, range[1])];
    return safeRange;
}

window.MyMath =
{
    random: function (start, end)
    {
        return start + Math.random() * (end - start);
    },

    min: function (dataValues, range)
    {
        if (!dataValues) return null;
        range = getSafeRange(dataValues, range) || this._defaultRange(dataValues);

        var min = dataValues[range[0]];
        for (var i = range[0]; i <= range[1]; i++)
        {
            min = Math.min(min, dataValues[i]);
        }
        return min;
    },

    max: function (dataValues, range)
    {
        if (!dataValues) return null;
        range = getSafeRange(dataValues, range) || this._defaultRange(dataValues);

        var max = dataValues[range[0]];
        for (var i = range[0]; i <= range[1]; i++)
        {
            max = Math.max(max, dataValues[i]);
        }
        return max;
    },

    median: function (dataValues, range)
    {
        if (!dataValues) return null;
        range = getSafeRange(dataValues, range) || this._defaultRange(dataValues);

        var middle = (range[1] + range[0]) / 2;
        var median = (dataValues[Math.floor(middle)] + dataValues[Math.ceil(middle)]) / 2;
        return median;
    },

    _defaultRange: function (dataValues)
    {
        if (!dataValues) return null;
        return [0, dataValues.length - 1];
    }
};
